package com.blb.service.admin.impl;

import com.blb.dao.admin.BrandDao;
import com.blb.dao.admin.impl.BrandDaoImpl;
import com.blb.pojo.Brand;
import com.blb.pojo.Page;
import com.blb.pojo.ProductType;
import com.blb.service.admin.IBrandService;
import com.blb.utils.ApplicationContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements IBrandService {

    @Autowired
    private BrandDao brandDao ;

    @Override
    public Page<Brand> page(int page, int size) throws Exception {
        List<Brand> list = brandDao.selectPage(page, size);
        int count = brandDao.count();
        return new Page<Brand>(page, size, list, count);
    }

    @Override
    public void add(Brand brand) throws Exception {
        brandDao.insert(brand);
    }

    @Override
    public void deleteById(int id) throws Exception {
        brandDao.deleteById(id);
    }

    @Override
    public void batchDelete(String[] ids) throws Exception {
        brandDao.batchDelete(ids);
    }

    @Override
    public Brand getById(int id) throws Exception {
        return brandDao.selectById(id);
    }

    @Override
    public void update(Brand brand) throws Exception {
        Brand brand1 = brandDao.selectByName(brand.getBrandName());
        if (brand1 != null && brand1.getId()!=brand.getId()){
            throw new Exception("此顺序已经存在");
        }
        brandDao.update(brand);
    }

    @Override
    public List<ProductType> getPType() throws Exception {
        List<ProductType> list = brandDao.getPType();
        return list;
    }

    @Override
    public List<Brand> getByTypeId(int productTypeId) throws Exception {
        return brandDao.selectByTypeId(productTypeId);
    }
}
